package com.lzy.mall.product.controller;


import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.common.utils.ResultUtils;
import com.lzy.mall.common.vo.AddToCartVo;
import com.lzy.mall.product.entity.PmsSkuInfo;
import com.lzy.mall.product.service.PmsSkuInfoService;
import com.lzy.mall.product.service.PmsSkuSaleAttrValueService;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * sku信息(PmsSkuInfo)表控制层
 *
 * @author lzy
 * @since 2021-08-25 11:41:20
 */
@RestController
@RequestMapping("skuinfo")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class PmsSkuInfoController {
    private final PmsSkuInfoService skuInfoService;

    private final PmsSkuSaleAttrValueService skuSaleAttrValueService;

    @GetMapping("/list")
    public ResultUtils list(@RequestParam Map<String, Object> params){
        PageUtils page = skuInfoService.queryPageByCondition(params);

        return ResultUtils.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{skuId}")
    public ResultUtils info(@PathVariable("skuId") Long skuId){
        PmsSkuInfo skuInfo = skuInfoService.getById(skuId);
        return ResultUtils.ok().setData(skuInfo);
    }

    @GetMapping("/{skuId}/price")
    public ResultUtils getPrice(@PathVariable("skuId") Long skuId){
        PmsSkuInfo byId = skuInfoService.getById(skuId);

        return ResultUtils.ok(byId.getPrice());
    }

    @GetMapping("/stringlist/{skuId}")
    public List<String> getSkuSaleAttrValue(@PathVariable("skuId") Long skuId){
        return skuSaleAttrValueService.getSkuSaleAttrValuesAsString(skuId);
    }

    @PostMapping("getSkuIdByAttr")
    public Long getSkuIdByAttr(@RequestBody AddToCartVo vo){
        return skuInfoService.getSkuIdByAttr(vo);
    }

}
